package cn.cdeden.knowledge.domain.dto;

import cn.cdeden.common.core.utils.MapstructUtils;
import com.alibaba.fastjson.annotation.JSONField;
import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

@Data
@AutoMapper(target = UserHideInfoDto.class, reverseConvertGenerate = false)
public class FileUserDto extends CommonDto implements Serializable {
    private static final long serialVersionUID = 1L;

    private Long id;

    private Set<RoleSmallDto> roles;

    private Set<JobSmallDto> jobs;

    private DeptSmallDto dept;

    private List<DeptDto> deptInfo;

    private Long deptId;

    private String username;

    private String nickName;

    private String gender;

    private String phone;

    private String email;

    private String avatarName;

    private String avatarPath;
    /**
     * minio桶名称
     */
    private String minioBucket;
    /**
     * 已用存储空间
     */
    private Long storageUse;
    /**
     * 储存空间
     */
    private Long storageSum;

    /**
     * 储存表id
     */
    private String storageId;

    /**
     * 头像网络地址
     */
    private String storageAvatarPath;

    /**
     * 储存服务器类型
     */
    private String storageType;

    /**
     * 工号
     */

    private String jobNumber;

    @JSONField(serialize = false)
    private Boolean isAdmin;

    private Boolean enabled;

    private Date pwdResetTime;


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        FileUserDto dto = (FileUserDto) o;
        return Objects.equals(id, dto.id) &&
            Objects.equals(username, dto.username);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, username);
    }


    public UserHideInfoDto getUserHideInfo() {
//        return ConvertUtil.convert(this, UserHideInfoDto.class);
        return MapstructUtils.convert(this, UserHideInfoDto.class);
    }

    public UserInfoQuery getUserInfoQuery() {
        Set<String> jobIds = new HashSet<>();
        if (CollectionUtils.isNotEmpty(jobs)) {
            jobIds = jobs.stream().map(i -> String.valueOf(i.getId())).collect(Collectors.toSet());
        }
        Set<String> roleIds = new HashSet<>();

        if (CollectionUtils.isNotEmpty(roles)) {
            roleIds = roles.stream().map(i -> String.valueOf(i.getId())).collect(Collectors.toSet());
        }
        String deptId = String.valueOf(this.deptId);
        Set<String> deptIds = new HashSet<>();
        if (CollectionUtils.isNotEmpty(deptInfo)) {
            deptIds = deptInfo.stream().map(i -> String.valueOf(i.getId())).collect(Collectors.toSet());
        }

        deptIds.add(deptId);
        return UserInfoQuery.builder()
            .userId(String.valueOf(id))
            .jobIds(jobIds)
            .roleIds(roleIds)
            .deptId(deptId)
            .deptIds(deptIds)
            .build();
    }
}
